package com.pengchuang.ACS.face.controller;

import cn.hutool.core.lang.UUID;
import com.pengchuang.ACS.common.constant.GlobalConstants;
import com.pengchuang.ACS.common.utils.PageUtils;
import com.pengchuang.ACS.common.utils.R;
import com.pengchuang.ACS.face.service.DoorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @projectName: AccessControlSystem
 * @see: com.pengchuang.ACS.face.controller
 * @author: pc
 * @createTime: 2023/3/18 15:13
 * @version: 1.0
 */
@RestController
@RequestMapping("face/terminal")
public class TerminalController {

	@Autowired
	private DoorService doorService;

	/**
	 * 列表
	 */
	@RequestMapping("/valid")
	public R valid(@RequestParam Map<String, Object> params){

		return doorService.validCode(String.valueOf(params.get("code")));

	}


	@RequestMapping("/face_valid")
	public R validFace(@RequestParam MultipartFile file, @RequestParam String code) {
		// 存放图片
		UUID uuid = UUID.randomUUID();
		String fileName = file.getOriginalFilename();
		String suffixName = fileName.substring(fileName.lastIndexOf("."));//后缀名
		String imagePath = uuid + suffixName;//图片的url
		File dest = new File(GlobalConstants.TEMP_FILEPATH + imagePath);
		try {
			file.transferTo(dest);
		} catch (IOException ignored) {
			return R.error();
		}

		return doorService.validFace(imagePath, code);
	}


}
